import { timStore } from '../../store/tim'
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { getDataSet } from '../../utils/utils'
import cache from '../../enum/cache'
import { setTabBarBadge } from '../../utils/wx'
// pages/message/message.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		conversationList: [],
		updateConversationList: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.storeBindings = createStoreBindings(this, {
			store: timStore,
			fields: ['sdkReady', 'conversationList'],
			actions: ['getConversationList'],
		})
	},
	onShow: function () {
		if (this.data.updateConversationList) {
			this.getConversationList()
			this.data.updateConversationList = false
		}

		const unreadCount = wx.getStorageSync(cache.UNREAD_COUNT)
		setTabBarBadge(unreadCount)
	},
	onunload: function () {
		this.storeBindings.destoryStoreBindings()
	},
	handleToLogin: function () {
		wx.navigateTo({
			url: '/pages/login/login',
		})
	},
	handleSelect(event) {
		this.data.updateConversationList = true
		const item = getDataSet(event, 'item')
		wx.navigateTo({
			url: `/pages/conversation/conversation?targetUserId=${item.userProfile.userID}&service=`,
			event: {
				sendMessage: () => {
					this.data.updateConversationList = false
				},
			},
		})
	},
})
